package com.cimpm.permission.service.impl;

import com.cimpm.permission.entity.DataPermission;
import com.hby.common.annotation.Remind;
import com.hby.common.callback.AutoProcessControllerPart;
import com.hby.common.entities.aspect.parameter.ParamConverter;
import com.hby.common.entities.aspect.parameter.ServiceInfo;
import com.hby.common.services.Impl.mian.AutoEntityServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataPermissionServiceImpl extends AutoEntityServiceImpl<DataPermission> {

    @Autowired
    private UserDataPermissionMapperServiceImpl userDpMapperService;
    @Autowired
    private DataPermissionTypeServiceImpl dataPermissionTypeService;

    @Override
    public void customService(AutoProcessControllerPart autoProcessControllerPart, ServiceInfo serviceInfo) throws Throwable {

    }

    @Override
    public void addEntityOtherToRedis(DataPermission entity) {

    }

    @Override
    public void delEntityOtherFromRedis(DataPermission entity) {

    }

    public Object getTypes(ParamConverter param,Integer userId,String userName) throws Throwable{
        return dataPermissionTypeService.getAllEntities();
    }

    public Object getPermsByUser(ParamConverter param, Integer userId, String userName) throws Throwable{
        Integer userIdSearch = param.getInt("userId");
        List<DataPermission> dataPermissions = userDpMapperService.getSecondaryEntitiesFromPrimaryId(userIdSearch);
        return dataPermissions;
    }

    public Object getPermsByType(ParamConverter param,Integer userId,String userName) throws Throwable{
       return dataPermissionTypeService.getAllEntities();
    }

    @Remind("true/false为验证是否存在，null为default权限")
    public Boolean verifyDataPerm(Integer userId,Integer type,String name){
        boolean res = false;
        boolean isDefault = true;
        List<DataPermission> dataPermissions = userDpMapperService.getSecondaryEntitiesFromPrimaryId(userId);
        for (DataPermission dp:dataPermissions){
            if (dp.getType().equals(type)){
                isDefault = false;
                if (dp.getName().equals("name")){
                    res = true;
                    break;
                }
            }
        }
        if (isDefault) return null;
        return res;
    }

    @Remind("null为default权限")
    public String getDataPerm(Integer userId,Integer type){
        List<DataPermission> dataPermissions = userDpMapperService.getSecondaryEntitiesFromPrimaryId(userId);
        for (DataPermission dp:dataPermissions){
            if (dp.getType().equals(type)){
                return dp.getName();
            }
        }
        return null;
    }
}
